<?php
class admin extends Controller {
	
	function welcome(){
		$this->loadView('admin_center/login');
	}
	function admin_center(){ 
		$this->loadView('admin_center/main');
	}
	function top(){
		$this->loadView('admin_center/top');
	}
	function center(){
		$this->loadView('admin_center/center');
	}
	function down(){
		$this->loadView('admin_center/down');
	}
	function tab(){
		$this->loadView('admin_center/tab');
	}
	function left(){
		$this->loadView('admin_center/left');
	}
    
	function checklogin(){
		if($_POST['formhas'] && isset($_POST['formhas']) && !is_null($_SESSION['formrand'])){
			$result = $_POST['formhas'] === $_SESSION['formrand'];
			if($result == false)exit('please input the right webaddress,try again!');
			else {
				$userCode = Admin_model::selectCode($_POST['username']);
				
				if($userCode){
				$keyCode = new SysCrypt($userCode['checkcode']);
				$userPass = $keyCode->php_decrypt($userCode['password']);
				$result = $userPass === $_POST['password'];
				
				if($result) {$_SESSION['admin'] = $_POST['username']; $this->admin_center();} 
				else $this->welcome();
				}
			}
		} else die('the formhas is null');
	}
	
	function database_list(){
		$data_path= ROOT.'./result/database/';  
		if(isset($data_path) && is_dir($data_path)){
		$database_list = skyIo::readDirs($data_path,'.sql');
		}	
		$this->loadView('admin_center/database_list',array('database_list'=>$database_list));
	}
	
	function lookdata($filename){
		$path = ROOT.'./result/database/'.$filename[0].'.sql';
		if($path && file_exists($path)){
		$content = file_get_contents($path);
		$this->loadView('admin_center/editor_sql',array('content'=>$content));
		}
	}
    
    function addNews(){
		$style = Admin_model::get_style();
		$this->loadView('admin_center/addNews',array('admins'=>$_SESSION['admin'], 'style'=>$style));
    }
    
    function save_news(){
		$news = array();
           $news['title'] = $_POST['newstitle']; $news['style'] = $_POST['newstype'];$news['content'] =htmlspecialchars($_POST['content']); $news['current_date']= date('Y-m-d');
           $news['endtime']=$_POST['endtime'];$news['publices'] = $_POST['member'] ? 1 : 0;  $news['admins'] = $_POST['admins'];
           $counts = Admin_model::insert_new($news);

           if($counts)  $this->message('default','success','addNews');
          else die('errors');
    }
    
    function add_news_type(){
		$style = Admin_model::get_style();
		$style_counts = Admin_model::get_style_count();
        // var_dump($style_counts['counts']);
           $this->loadView('admin_center/add_news_type', array('admins'=>$_SESSION['admin'],'style'=>$style,'style_counts'=>$style_counts['counts']));
    }
    
    function editor_style($urlParams){
	list($id,$sty_name) = $urlParams;
	if(isset($id) && is_numeric($id)){
		echo Admin_model::update_sty($id,$sty_name);
        }
    }
}
?>
